<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Specification extends BaseModel
{
    use HasFactory;
    protected $table = 'specification';
    // 允许批量操作的字段
    protected $fillable = ['specification','is_del','image','created_at','updated_at'];

    public function scopeSearch($query,$params)
    {
        if(!empty($params['specification'])){
            $query->where('specification','like','%'.$params['specification'].'%');
        }
        $query->where('is_del',$params['is_del']??2);

        if(isset($params['field_list']) && !empty($params['field_list'])){
            $select_arr = explode(',',$params['field_list']);
        }else{
            $select_arr = "*";
        }
        $query->select($select_arr);
        return $query;
    }

    public function model_number(){
        return $this->belongsToMany(ModelNumber::class,'model_number_specification','model_number_id','specification_id');
    }

    public function product(){
        return $this->belongsToMany(Product::class,'model_number_specification','model_number_id','specification_id');
    }

    public function center(){

    }


}
